import {ComponentInternalInstance, getCurrentInstance, reactive} from "vue";

const state = {
    tagsList: [],
    tagsname: [],
    cachedViews: [],
    sidebarRouters: [],
    iframeSrc: '',
    iframeTitle: ''
}
const home: any = reactive({
    name: "resource",
    path: "/admin/dashboard",
    title: "系统首页"
})

const mutations = {
    ADD_VISITED_VIEW: (state: any, view: any) => {
        if (state.tagsList.length == 0) {
            state.tagsList.push(home)
        }

        if (view.meta.keepAlive) {
            if (home.path != view.path) {
                state.tagsList.push({
                    title: view.meta.title,
                    path: view.path,
                    query:view.query,
                    fullPath:view.fullPath,
                    name: view.name ? view.name : view.matched[1].components.default.name,
                    matched: view.matched ? view.matched : []
                })
            }
            let arr = view.matched
            if (arr != undefined) {
                for (let i = 0; i < arr.length; i++) {
                    if (!state.tagsname.includes(arr[i].name)) {
                        state.tagsname.push(arr[i].name)
                    }
                }
            }
        }
    },

    DEL_VISITED_VIEW: (state: any, index: any) => {
        let delitem = state.tagsList[index]
        state.tagsList.splice(index, 1)[0];
        state.tagsname = state.tagsname.filter(item => !delitem.matched.map(e => e.name).includes(item))
    },
    DEL_ROUTER_VIEW:(state: any, view: any)=>{
        for (let i = 0; i < state.tagsList.length ; i++) {
            if(state.tagsList[i].path === view.path){
                let delitem = state.tagsList[i]
                state.tagsList.splice(i, 1)[0];
                state.tagsname = state.tagsname.filter(item => !delitem.matched.map(e => e.name).includes(item))
                break
            }
        }
    },
    DEL_OTHERS_VISITED_VIEWS: (state: any, view: any) => {
        state.tagsname = []//清空缓存
        if (home.path != view.path) {
            const curItem = state.tagsList.filter(item => {
                return item.path === view.path;
            })
            curItem.unshift(home)
            state.tagsList = curItem;
            if (view.meta.keepAlive) {
                let arr = view.matched
                if (arr != undefined) {
                    for (let i = 0; i < arr.length; i++) {
                        if (!state.tagsname.includes(arr[i].name)) {
                            state.tagsname.push(arr[i].name)
                        }
                    }
                }
            }
        }
    },

    DEL_ALL_VISITED_VIEWS: (state: any) => {
        state.tagsList = [home];
        state.tagsname = []

    },
    SET_SIDEBAR_ROUTERS: (state: any, routes: any) => {
        state.sidebarRouters = routes
    },
    SET_IFRAMESRC: (state: any, iframeSrc: any) => {
        state.iframeSrc = iframeSrc
    },
    SET_IFRAMETITLE: (state: any, iframeTitle: any) => {
        state.iframeTitle = iframeTitle
    }
}

const actions = {
    addVisitedView({commit}, view) {
        commit('ADD_VISITED_VIEW', view)
    },
    delRouterView({commit, state}, view) {
        commit('DEL_ROUTER_VIEW', view)
        return new Promise(resolve => {
            resolve({
                visitedViews: [...state.tagsList],
            })
        })
    },
    delVisitedView({commit, state}, view) {
        return new Promise(resolve => {
            commit('DEL_VISITED_VIEW', view)
            resolve([...state.tagsList])
        })
    },
    delOthersVisitedViews({commit, state}, view) {
        return new Promise(resolve => {
            commit('DEL_OTHERS_VISITED_VIEWS', view)
            resolve([...state.tagsList])
        })
    },

    delAllVisitedViews({commit, state}) {
        return new Promise(resolve => {
            commit('DEL_ALL_VISITED_VIEWS')
        })
    },
    setsidebarRouters: ({commit}, routes) => {
        commit('SET_SIDEBAR_ROUTERS', routes)
    },
    set_iframesrc: ({commit}, iframeSrc) => {
        commit('SET_IFRAMESRC', iframeSrc);
    },
    set_iframeTitle: ({commit}, iframeTitle) => {
        commit('SET_IFRAMETITLE', iframeTitle);
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}